package com.yloveg.common;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class DateUtil {

	/**
     * 获得20150803 --> 15/8/3,15/12/6,15/2/15,15/10/3文件夹形式
     *
     * @param date
     * @return 15/10/3文件夹形式
     */
    public static String endFileDir () {
          Date date = new Date(System. currentTimeMillis());
          SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd" );
          String str = sdf.format(date).toString();
          StringBuffer sb = new StringBuffer();
           char[] timeArr = str.toCharArray();
          sb = sb.append(timeArr[2]).append(timeArr[3]);
           // str = ""+timeArr[2]+timeArr[3];
           if (timeArr[4] != '0') {
               sb = sb.append(timeArr[4]);
                // str+=timeArr[4];
          }
          sb = sb.append(timeArr[5]).append( "/");
           // str+=""+timeArr[5]+"/";//根据当前系统环境确定分隔符
          
          
           //确定天数作为文件夹,测试部不需要天数，直接注释即可
           if(timeArr[6]!= '0'){
               sb = sb.append(timeArr[6]);
          }
          sb = sb.append(timeArr[7]).append( "/");
           return sb.toString().trim();
    }
    
    /**
     * @return yyyyMMddhhmmssSSS
     */
    public static String transPath() {
      Date date = new Date();
      SimpleDateFormat dateformat = new SimpleDateFormat("yyyyMMddhhmmssSSS");// 定义到毫秒
      String nowStr = dateformat.format(date);
      return nowStr;
    }
    
    /**
     * 获取两个时间直接相差多少分钟
     * @param endDate时间早的
     * @param nowDate时间晚的
     * @return
     */
	public static Long getDatePoor(Date endDate, Date nowDate) {

		long nd = 1000 * 24 * 60 * 60;

		long nh = 1000 * 60 * 60;

		long nm = 1000 * 60;

		// long ns = 1000;

		// 获得两个时间的毫秒时间差异

		long diff = endDate.getTime() - nowDate.getTime();

		// 计算差多少天

		long day = diff / nd;

		// 计算差多少小时

		long hour = diff % nd / nh;

		// 计算差多少分钟

		long min = diff % nd % nh / nm;

		// 计算差多少秒//输出结果

		// long sec = diff % nd % nh % nm / ns;

		return diff;
	}
	
	/**
     * 获取两个时间直接相差多少分钟
     * @param endDate时间早的
     * @param nowDate时间晚的
     * @return
     */
	public static Long getDateDay(Date endDate, Date nowDate) {

		long nd = 1000 * 24 * 60 * 60;

		long nh = 1000 * 60 * 60;

		long nm = 1000 * 60;

		// long ns = 1000;

		// 获得两个时间的毫秒时间差异

		long diff = endDate.getTime() - nowDate.getTime();

		// 计算差多少天

		long day = diff / nd;

		// 计算差多少小时

		long hour = diff % nd / nh;

		// 计算差多少分钟

		long min = diff % nd % nh / nm;

		// 计算差多少秒//输出结果

		// long sec = diff % nd % nh % nm / ns;

		return day;
	}
	
	public static String  timesBetween(Date endDate, Date nowDate) {
	    long day = 0;  
        long hour = 0;  
        long min = 0;  
        long sec = 0;  
        try {  
            long time1 = endDate.getTime();  
            long time2 = nowDate.getTime();  
            long diff ;  
            if(time1 < time2) {  
                diff = time2 - time1;  
            } else {  
                diff = time1 - time2;  
            }  
            day = diff / (24 * 60 * 60 * 1000);  
            hour = (diff / (60 * 60 * 1000) - day * 24);  
            min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);  
            sec = (diff/1000-day*24*60*60-hour*60*60-min*60);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return day + "天" + hour + "小时" + min + "分" + sec + "秒";  
	}
	
}
